home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 3496 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.4 KB  |  64 lines

  1. Path: damon.irf.uni.dortmund.de!broth
  2. From: rothert@damon.irf.uni-dortmund.de (Bernd Rothert)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Borland C++ 4.52 -  problems with tellg & ftell
  5. Date: Wed, 24 Jan 96 12:45:21 GMT
  6. Organization: Institute of Robotics Research
  7. Message-ID: <4e59r8$bb4@damon.irf.uni-dortmund.de>
  8. References: <4dk1hm$se7@kettle.magna.com.au>
  9. NNTP-Posting-Host: broth.irf
  10. X-Newsreader: News Xpress Version 1.0 Beta #4
  11.  
  12. In article <4dk1hm$se7@kettle.magna.com.au>,
  13.    techteam@sratem.com.au (Technical Team) wrote:
  14. >PROBLEM WITH istream:: tellg() using Borland C++ 4.52 WIN 32
  15. >Has anyone else had problems using tellg or ftell using Borland with 
  16. >WIN 32 ?
  17. Yes - I also had problems parsing a DOS text file using fstreams because 
  18. tellg() modified the current stream position (BC++4.5 and MSVC++1.51).
  19.  
  20. Stream handling and especially tellg() seems to be a problem for most DOS C++ 
  21. Compilers because of the special handling of the CR/LF sequence in text mode.
  22.  
  23. The fstream classes open files in text mode by default. So each CR/LF pair 
  24. should count as one '\n' (char 10) as it is when reading the file one 
  25. character at a time. Therefore tellg() should return the number of characters 
  26. from the beginning of the file with CR/LF = 1 char, not the physical position.
  27.  
  28. Try the following small program - it fails on most DOS compilers UNLESS YOU 
  29. USE ios::binary IN THE CTOR OF THE ifstream (commented out below).
  30. The bad news is that you have to handle CR/LF on your own in binary mode.
  31.  
  32. ---cut here---
  33. #include <fstream.h>
  34. const char FNTEST[] = "test.tmp";
  35. const char DIGITS[] = "0123456789";
  36. void digits(ostream& strm, int n) {
  37.     for (int i = 0; i < n; i++)
  38.         strm << DIGITS[i % 10];
  39.     strm << endl;
  40. }
  41. int main(int argc, char*[]) {
  42.   if (argc < 2)
  43.     digits(ofstream(FNTEST), 512); // create text mode file with CR/LF
  44.   else
  45.     digits(ofstream(FNTEST, ios::binary), 512); // binary/Un*x file with LF
  46.   digits(cout, 10);
  47.   ifstream istrm(FNTEST /*, ios::binary */); // remove comment to make it work
  48.   for (int i = 0; i < 10; i++) {
  49.     istrm.tellg();
  50.     cout << (char)istrm.get();
  51.   }
  52.   return 0;
  53. }
  54. /* Results (option "-ml"):
  55.             CR/LF DOS Text    Un*x LF only
  56. BC++ 4.5    0135791357          0123456789 (ok)
  57. BC++ 3.1    0123456789 (ok)     0111111111
  58. MSVC++ 1.51 0134567890          0111111111
  59. MSVC++ 2.2  0134567890          0111111111
  60. WATCOM 10.5 0123456789 (ok)     0111111111
  61. */
  62. ---cut here---
  63.  
  64.